#include "TcpServer.hpp"
#include "Socket.hpp"
#include "Accepter.hpp"
#include <iostream>

using namespace NetWork;

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        std::cout << "Usage: " << argv[0] << " port" << std::endl;
        return 1;
    }

    int port = std::stoi(argv[1]);

    // 1. bulid listen socket
    std::unique_ptr<Socket> listensock = std::make_unique<TcpSocket>();
    listensock->BuildListenSocketMethod(port);

    // 2. create TcpServer and Accepter object
    auto server = std::make_shared<TcpServer>();
    auto accepter = std::make_unique<Accepter>();

    // 3. create listen_resver function for buliding listen connection
    auto listen_resver = std::bind(&Accepter::acceptConnection, std::placeholders::_1);

    // 4. create listen connection
    uint32_t events = EPOLLIN | EPOLLET;
    auto listen_conn = ConnectionFactory::buildListenConnection(
        listensock->GetSockfd(), events, server, listen_resver);

    // 5. add listen connection to server
    server->addConnection(listen_conn);

    // 6. start dispatch by server(reactor)
    server->dispatch();
    
    return 0;
}

